package TUILayer;
import java.util.Scanner;
import java.util.Iterator;
import ControlLayer.DVDCtr;
import ModelLayer.Dvd;
import ModelLayer.Copy;

/**
 * User interface to the DVD controller
 * @author(group1)
 * @version(2013.11.05)               
 */

public class DvdUI extends InputOutput
{
    private DVDCtr dvdCtr;

    public DvdUI() {   dvdCtr = DVDCtr.getInstance();   }

    public void start()  { dvdMenu(); }

    public void dvdMenu()
    {   
        boolean exit = false;
        while(!exit)
        {
            int choice =   writeDVDMenu();
            System.out.println("\f");
            switch(choice)
            {
                case 1 : findDVD();break;
                case 2 : createDVD();break;
                case 3 : createCopy();break;
                case 4 : deleteDVD();break;
                case 5 : updateDVD();break;
                case 6 : listAllDvds();break;
                default : exit=true; break;
            }
        }
    }

    private int writeDVDMenu()
    {
        System.out.println("\n *** DVD Menu ***");
        System.out.println(" (1) Show DVD information");
        System.out.println(" (2) Create DVD");
        System.out.println(" (3) Create Copy");
        System.out.println(" (4) Delete DVD");
        System.out.println(" (5) Update DVD");
        System.out.println(" (6) Show DVD list");
        System.out.println(" (7) Return");
        System.out.print("\n Make your choice: ");
        
        return readInt();
    }


    private void findDVD()
    {     
        long id = inputLong("DVD id");
        Dvd dvd = dvdCtr.getDVD(id);
        printDvdInfo(dvd);
    }

    private void createDVD()
    {
        long id = inputLong("id");
        String title = inputString("Title of DVD");
        String artist = inputString("Artist");
        String publicationDate = inputString("Publication date of DVD");
        String description = inputString("Description of DVD"); 

        dvdCtr.createDVD(id, title, artist, publicationDate, description);
        System.out.println(" --> The DVD was successfully created.");
    }
    
    private void updateDVD()
    {
        long id = inputLong("id");
        String title = inputString("Title");
        String artist = inputString("Artist");
        String description = inputString("Description");
        
        if(dvdCtr.updateDVD(id,title,artist,description))
            System.out.println(" --> The DVD with id " + id + " was successfully updated.");
        else
            System.out.println(" --> An error occurred while updating the DVD with id " + id);
    }

    private void createCopy()
    {
        long id = inputLong("id of DVD");
        String serialNumber = inputString("Serial Number");
        double purchasePrice = inputDouble("Purchase Price");
        if(dvdCtr.createCopy(id,serialNumber, purchasePrice))
            System.out.println(" --> The copy was successfully created.");
        else
            System.out.println(" --> An error occurred while creating the copy. ");
    }

    private void deleteDVD()
    {
        long id = inputLong("id");
        if(dvdCtr.deleteDVD(id))
            System.out.println(" --> The DVD with ID "+id+" was successfully deleted."); 
        else    
            System.out.println(" --> The DVD with ID "+id+" does not exist."); 
    }

    private void listAllDvds()
    {
        Iterator it = dvdCtr.listAllDvds();

        if(it == null) { System.out.println(" --> The DVD list is empty."); return; }
        
        while (it.hasNext())
        {
            Dvd dvd = (Dvd)it.next();
            System.out.println(" Id: " + dvd.getId() + " Title: " + dvd.getTitle());
        }
    }
    
    private void printDvdInfo(Dvd dvd)
    {
        if(dvd == null) {System.out.println(" --> This DVD does not exist!"); return;}
        
        System.out.println();
        System.out.println(" ###### Movie Info ######");
        System.out.println(" Title: " + dvd.getTitle());
        System.out.println(" Artists: " + dvd.getArtist());
        System.out.println(" Description: " + dvd.getDescription());
        Iterator dvdCopies = dvdCtr.listDvdCopies(dvd.getId());
        
        if(dvdCopies.hasNext()==false) { System.out.println(" --> There is no copies created.");  return; } 
        
        int i=0;
        
        while(dvdCopies.hasNext())
        {
            Copy copy=(Copy)dvdCopies.next();
            System.out.println(" --> Copie no. "+(++i)+"");
            System.out.println("     Serial no.: " + copy.getSerialNumber());
            System.out.println("     Purchase price: " + copy.getPurchasePrice());
            System.out.println("     Purchase date: " + copy.getPurchaseDate());
        }
            
        System.out.println();
    }
}